import { nextmeshEventCenter } from '@app-nextmesh/common/events/EventCenter';
import Base from '../cmd-common/base';
import { CENTEREVENTS, EventsRes } from '@amcax/base';
import { createPoint, removePoint } from '../cmd-common/tools';
import { ctx2 } from '@app-base/module';

export default class CmdInteractor extends Base {
  setParam;
  constructor({ setParam }) {
    super();
    this.setParam = setParam;
    this.registerEvents();
  }
  registerEvents = () => {
    nextmeshEventCenter()
      .setState('cmdDrillHole')
      .on(CENTEREVENTS.POINTERMOVE, this.onMouseMove)
      .on(CENTEREVENTS.POINTERDOWN, this.onMouseUp)
      .on(CENTEREVENTS.KEYUP, this.onKeyUp);
  };
  unregisterEvents = () => {
    nextmeshEventCenter()
      .off(CENTEREVENTS.POINTERMOVE, this.onMouseMove)
      .on(CENTEREVENTS.POINTERDOWN, this.onMouseUp)
      .off(CENTEREVENTS.KEYUP, this.onKeyUp)
      .addExit(this.destroy)
      .setDefault();
  };
  onMouseMove = (e: EventsRes) => {
    this.mouseMove(e, false);
  };
  onMouseUp = (e: EventsRes) => {
    if (e.button.onLeft && this.point) {
      if (this.pickInfo?.assisObj.name === 'pickedFace') {
        this.renderPoint();
        this.setParam(() => {
          return {
            source: this.point?.toArray(),
            target: [this.pickInfo?.meshId],
            pickName:
              this.pickInfo?.objInfo?.label +
              '上的面' +
              this.pickInfo.idInfo?.i +
              ' 的点：',
            positionText:
              'x:' +
              this.point.x.toFixed(2) +
              ' y:' +
              this.point.y.toFixed(2) +
              ' z:' +
              this.point.z.toFixed(2),
          };
        });
      }
    }
  };
  resetPoint() {
    removePoint();
    this.point = null;
  }
  renderPoint = () => {
    removePoint();
    createPoint([this.point]);
    ctx2.viewer.requestRender();
  };
  destroy(): void {
    this.unregisterEvents();
  }
}
